﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CodeGenerator.Models;
using System.Globalization;

namespace CodeGenerator.Util
{
    internal static class StringExt
    {
        public static bool IsNumber(this string text)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(text);
        }

        public static string WriteAttributeWithDefaultValue(AttributeModel attr)
        {
            string result = "";
            Tuple<bool, string> temp = DataTypeMapper.IsPrimitiveDataType(attr);
            if (temp.Item1)
            {
                if (attr.DataType.TypeId == 15)
                {
                    result = "model." + String.Format("{0} = \"{1}\";", attr.Name, attr.DefaultValue);
                }
                else if (attr.DataType.TypeId == 11)
                {
                    result = "model." + String.Format("{0} = {1}m;", attr.Name, attr.DefaultValue);
                }
                else if (attr.DataType.TypeId == 14)
                {
                    string value = ReplaceDateDefaultValue(string.IsNullOrWhiteSpace(attr.DefaultValue) ? (attr.Readonly ? "today()" : "null") : attr.DefaultValue);

                    if (!value.Equals("null"))
                        result = "model." + String.Format("{0} = {1};", attr.Name, value);
                }
                else
                {
                    result = "model." + String.Format("{0} = {1};", attr.Name, attr.DefaultValue);
                }
            }
            return result;
        }

        private static string ReplaceDateDefaultValue(string value)
        {
            if (value.Equals("today()", StringComparison.OrdinalIgnoreCase) || value.Equals("@today()", StringComparison.OrdinalIgnoreCase))
                return "DateTime.Today";
            if (value.Equals("bom()", StringComparison.OrdinalIgnoreCase) || value.Equals("@bom()", StringComparison.OrdinalIgnoreCase))
                return "DateTime.ParseExact(DateTime.Today.ToString(\"01/MM/yyyy\"), \"dd/MM/yyyy\", System.Globalization.CultureInfo.InvariantCulture)";
            if (value.Equals("eom()", StringComparison.OrdinalIgnoreCase) || value.Equals("@eom()", StringComparison.OrdinalIgnoreCase))
                return "DateTime.ParseExact(DateTime.Today.ToString(\"01/MM/yyyy\"), \"dd/MM/yyyy\", System.Globalization.CultureInfo.InvariantCulture).AddMonths(1).AddDays(-1)";
            
            DateTime dt;

            try
            {
                dt = DateTime.ParseExact(value, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                return "DateTime.ParseExact(\"" + value + "\", \"dd/MM/yyyy\", System.Globalization.CultureInfo.InvariantCulture)";
            }
            catch{
                if (DateTime.TryParse(value, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                    return "DateTime.Parse(\"" + dt.ToString("dd/MM/yyyy") + "\")";
            }
            
            return "null";
        }
    }
}
